import java.awt.BorderLayout;

import javax.swing.JApplet;
import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;

//EXPORT TO EXCEL IS NOT WORKING NOW - NEED TO ACCESS THE BROWSER'S SAVE MENU!!
//Try signing the applet as the first fix. See notes slide 4 Lecture 23

/**
 * Creates the GUI to view data from the federated database.
 * @author jcoston
 */
public class ReswellMain extends JApplet {

	private static final long serialVersionUID = 1L;
	private Controls control;
	private Table table;
	private CSVModel model;
	private Tree tree;
	

	/**
	 * Set up the frame with its components
	 */
	public void init()
	{
		//Set up the frame with the title "Reswell"
		//super("RESWELL");
		setLayout(new BorderLayout(0, 5));
		
		//Change the layout to Nimbus
		try {
		    for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
		        if ("Nimbus".equals(info.getName())) {
		            UIManager.setLookAndFeel(info.getClassName());
		            break;
		        }
		    }
		} catch (Exception e) {
		    // If Nimbus is not available, you can set the GUI to another look and feel.
		}
		
		//Create and setup the window
		JFrame frame = new JFrame("RESWELL");
		
		/**
		 * TODO:
		 *   - when run from a browser, the JVM throws this error:
		 *      -> java.security.AccessControlException: Applets may not call System.exit()
		 *   - should find another method of closing or leave as default HIDE_ON_CLOSE
		 */
        //frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		//Create the data model for the table and controls
		model = new CSVModel();

		//Create the table
		table = new Table(model);
		
		//Create tree
		// Uncomment the below line for deployment
		tree = new Tree(frame, this.getParameter("jsonSchema"));
/*		tree = new Tree(frame,
				"[  {\"name\": \"ChillWELL\"," +
				"\"tables\": [ " +
					"{" +
						"\"fields\": [  " +
							"{\"alternate\": \"Entry Id\",\"fieldname\": \"Entry Id\"  }," +
							"{\"alternate\": \"1\",\"fieldname\": \"1\"}," +
							"{\"alternate\": \"2\",\"fieldname\": \"2\"}," +
							"{\"alternate\": \"3\",\"fieldname\": \"3\"} ]," +
						"\"tablename\": \"Day 1\"}," +
					"{" +
						"\"fields\": [ " +
							"{\"alternate\": \"Entry Id\",\"fieldname\": \"Entry Id\"  }," +
							"{\"alternate\": \"Smelled Wonderful\",\"fieldname\": \"Smelled Wonderful\"  }," +
							"{\"alternate\": \"Looked Beautiful\",\"fieldname\": \"Looked Beautiful\"  }," +
							"{\"alternate\": \"Felt Amazing\",\"fieldname\": \"Felt Amazing\"  }," +
							"{\"alternate\": \"Tasted Delicious\",\"fieldname\": \"Tasted Delicious\"  }]," +
						"\"tablename\": \"Day 12\"}]}]"
		);
*/
		
		//Create the controls
		control = new Controls(model, tree, table);
		
		
		//Add to the applet
		frame.add(control, BorderLayout.NORTH);
		frame.add(table, BorderLayout.CENTER);
		frame.add(tree, BorderLayout.WEST);
		
		frame.pack();
		frame.setVisible(true);

	}
	

	/**
	 * Start the GUI
	 * @param args
	 */
	public static void main(String[] args) {
		/*final ReswellMain res = new ReswellMain();
		//Dispose of the window upon closing
		//res.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent windowevent) {
				res.dispose();
				System.exit(0);
			}
		});
		//Set default size
		res.setSize(800, 800);
		res.setVisible(true);*/
		
	}

}
